#ifndef ____SSBASEL__SSBASE__BASE_BIN__H__
#define ____SSBASEL__SSBASE__BASE_BIN__H__
/*!
* \file base_bin.h
* \brief
* 
*
* 
* \author patdz@qq.com
* \version 1.0
* \date 2014/05/26
*/

#include "base.h"

_SSL_BEGIN
_SSBASEL_BEGIN

class SSBASE_CLASS ssbin
{
public:
	ssbin();
	ssbin(const ssbin &b);
	virtual ~ssbin();

public:
	//
	unsigned char *alloc_ptr(size_t sz); // alloc sz + 1 buffer and set the size is sz
	bool set_size(size_t new_size);

	//
	unsigned char *ptr() const;
	size_t size() const;

	//
	unsigned char* detach(size_t *sz = NULL);
	void clear();
public:
	ssbin& operator=(const ssbin& b);

protected:
	// the really buffer size is (sz + 1)
	bool makesure_buffer_is_enough(size_t sz);

protected:
	unsigned char *ptr_;
	size_t bin_size_;
	size_t allocat_size_;
};

template<typename T>
class ss_auto_buffer
{
public:
	ss_auto_buffer()
	{

	}

	~ss_auto_buffer()
	{

	}

	T* alloc_ptr(size_t sz_obj)
	{
		return (T*)buffer_.alloc_ptr(sz_obj * sizeof(T));
	}

	T* ptr()
	{
		return (T*)buffer_.ptr();
	}

	size_t size()
	{
		return buffer_.size() / sizeof(T);
	}

protected:
	ssbin buffer_;
};

_SSBASEL_END
_SSL_END

#endif
